<template>
    <page-layout>
        <!-- 头部 -->
        <template v-slot:header>
            <el-input placeholder="输入名字查询" size="small"></el-input>
            <el-select value="全部" size="small">
                <el-option value="1"></el-option>
            </el-select>
            <el-button type="primary" size="small">查询</el-button>
            <el-button size="small">新增</el-button>
        </template>

        <main class="main_flex">
        <!--表格部分  -->
        <el-table ref="elTable" class="main_table" :data="tableData" size="small" height="100%"  highlight-current-row 
        @current-change="handleCurrentChange">
            <el-table-column prop="date" label="日期" width="180">
            </el-table-column>
            <el-table-column prop="name" label="姓名" width="180">
            </el-table-column>
            <el-table-column prop="address" label="地址">
            </el-table-column>
            <el-table-column label="操作" fixed="right" width="180">
                <template slot-scope="scope">
                    <el-button type="text" size="small" @click="detailsClick(scope.row)">详情</el-button>
                    <el-button type="text" size="small" @click="delClick(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 树节点 -->
        <el-tree class="main_tree" show-checkbox ref="tree"
        node-key="path"
        :default-checked-keys="checkedKey"
        :default-expand-all="true" :data="treeData" :props="defaultProps" @node-click="handleNodeClick"></el-tree>
        </main>
        <!-- 底部 -->
        <template v-slot:footer>
            <el-pagination :page-sizes="[10, 20, 30, 40]" :page-size="query.size" :current-page="query.page"
                @size-change="sizeChange" @current-change="currentChange"
                layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination>
        </template>
    </page-layout>
</template>

<script>

export default {
    data() {
        return {
            tableData: [],
            currentRow: null,
            query: {
                name: '',
                state: '',
                size: 10,
                page: 1,
            },
            stateType: [],
            total: 0,
            timer: null,
            treeData: [],
            defaultProps: {
                children: 'children',
                label: 'name'
            },
            checkedKey: ['/userManagement']
        }
    },
    methods: {
        // 加载表格数据
        getList() {
            this.tableData = [
                {   
                    id: 1,
                    date: '2016-05-02',
                    name: '王小虎',
                    address: '上海市普陀区金沙江路 1518 弄',
                    checkedKey: ['/userManagement','/home'],
                }, {
                    id: 2,
                    date: '2016-05-04',
                    name: '张三',
                    address: '上海市普陀区金沙江路 1517 弄',
                    checkedKey: ['/userManagement'],
                }, {
                    id: 3,
                    date: '2016-05-01',
                    name: '李四',
                    address: '上海市普陀区金沙江路 1519 弄',
                    checkedKey: ['/userManagement'],
                }, {
                    id: 4,
                    date: '2016-05-03',
                    name: '老六',
                    address: '上海市普陀区金沙江路 1516 弄',
                    checkedKey: ['/userManagement'],
                }
            ]
            this.$nextTick(() => {
                this.$refs.elTable.setCurrentRow(this.tableData[0]);
            })
        },
        // 分页
        sizeChange(size) {
            this.query.size = size
            if (this.timer) {
                clearTimeout(this.timer)
            }
            // 防抖
            this.timer = setTimeout(() => {
                this.getList()
            }, 100)
        },
        currentChange(page) {
            this.query.page = page
            if (this.page) {
                clearTimeout(this.timer)
            }
            this.timer = setTimeout(() => {
                this.getList()
            }, 100)
        },
        detailsClick(row){
            // 详情
            console.log(row);
        },
        delClick(row){
            // 删除
            this.tableData.splice(this.tableData.indexOf(row), 1)
        },
        handleCurrentChange(val) {
            this.currentRow = val;
            this.checkedKey = val.checkedKey
            this.$refs.tree.setCheckedKeys(this.checkedKey);
        },
        handleNodeClick(data) {
            console.log(data);
        },
        // 获取树节点数据
        getTree() {
            let children = this.$router.options.routes.filter(it => it.path !== '/login' && it.path !== '/')
                .map(val=>{
                    val.children&&val.children.forEach(item=>{
                        item.path = val.path + item.path
                    })
                    return val
                })
                this.treeData = [
                    {
                        name: '所有菜单',
                        children,
                        path: '/'
                    }
                ]
                console.log('获取树节点数据',this.treeData);
        },
    },
    mounted() {
        this.getList()
        this.getTree()
    }
}
</script>

<style lang="less" scoped>
.main_flex{
    display: flex;
    .main_table{
        width: calc(100% - 200px);
    }
    .main_tree{
        width: 200px;
    }
}
</style>